const app = getApp()
let util = require('../../../common/util/util.js')
let text = require('../../../common/util/text.js')
let q = require('./evaluation.js')
Page({
    data: {
        xPageReady : 'going',
        bgColor:[],
        bgColorS:[],
        first:0,
        beforeList:[{name:"头顶",style:"top:0rpx;left:325rpx;width:100rpx;height:15rpx;"},
          {name:"右眼",style:"top:85rpx;left:320rpx;width:40rpx;height:40rpx;border-radius:20rpx;"},
          {name:"左眼",style:"top:85rpx;right:330rpx;width:40rpx;height:40rpx;border-radius:20rpx;",left:true},
          {name:"鼻",style:"top:130rpx;left:357rpx;width:30rpx;height:30rpx;"},
          {name:"口",style:"top:160rpx;left:357rpx;width:30rpx;height:30rpx;"},
          {name:"右耳",style:"top:110rpx;left:290rpx;width:20rpx;height:40rpx;"},
          {name:"左耳",style:"top:110rpx;right:300rpx;width:20rpx;height:40rpx;",left:true},
          {name:"前额",style:"top:15rpx;left:310rpx;width:120rpx;height:40rpx;border-radius:20rpx 20rpx 0 0;"},
          {name:"颈",style:"top:200rpx;left:315rpx;width:110rpx;height:40rpx;"},
          {name:"右肩",style:"top:250rpx;left:180rpx;width:100rpx;height:140rpx;border-radius:120rpx 20rpx 90rpx 60rpx;z-index:9;"},
          {name:"左肩",style:"top:250rpx;right:190rpx;width:90rpx;height:140rpx;border-radius:20rpx 90rpx 20rpx 80rpx  ;z-index:9;",left:true},
          {name:"右胸",style:"top:250rpx;left:250rpx;width:125rpx;height:230rpx;border-radius:0 0 70rpx  0rpx ;z-index:5;"},
          {name:"左胸",style:"top:250rpx;right:250rpx;width:125rpx;height:230rpx;border-radius:0 0 0rpx  70rpx; z-index:5;",left:true},
          {name:"右上臂",style:"top:390rpx;left:180rpx;width:70rpx;height:110rpx;z-index:6;"},
          {name:"左上臂",style:"top:390rpx;right:180rpx;width:70rpx;height:110rpx;z-index:6;",left:true},
          {name:"左前臂",style:"top:520rpx;right:135rpx;width:80rpx;height:230rpx;transform: rotate(-20deg);",left:true},
          {name:"右前臂",style:"top:520rpx;left:135rpx;width:80rpx;height:230rpx;transform: rotate(20deg); "},
          {name:"左上腹",style:"top:470rpx;right:260rpx;width:115rpx;height:80rpx;",left:true},
          {name:"右上腹",style:"top:470rpx;left:260rpx;width:116rpx;height:80rpx;"},
          {name:"左下腹",style:"top:550rpx;right:260rpx;width:115rpx;height:100rpx;",left:true},
          {name:"右下腹",style:"top:550rpx;left:260rpx;width:115rpx;height:100rpx;"},
          {name:"左手掌",style:"top:740rpx;right:60rpx;width:90rpx;height:150rpx;transform: rotate(-20deg);border-radius:0 0 100rpx  90rpx;",left:true},
          {name:"右手掌",style:"top:740rpx;left:60rpx;width:90rpx;height:150rpx;transform: rotate(20deg);border-radius:0 0 100rpx  90rpx ;"},
          {name:"左髋骨",style:"top:580rpx;right:250rpx;border-radius:100rpx 0  0 0 ;width:100rpx;height:130rpx;z-index:5",left:true},
          {name:"右髋骨",style:"top:580rpx;left:250rpx;border-radius:0 100rpx 0 0 ;width:100rpx;height:130rpx;z-index:5"},
          {name:"左大腿",style:"top:720rpx;right:240rpx;width:130rpx;height:300rpx;",left:true},
          {name:"右大腿",style:"top:720rpx;left:240rpx;width:130rpx;height:300rpx;"},
          {name:"生殖器官",style:"top:650rpx;left:340rpx;width:70rpx;height:70rpx;z-index:9;border-radius:"},
          {name:"左膝",style:"top:1030rpx;right:280rpx;width:90rpx;height:70rpx;border-radius:20rpx;",left:true},
          {name:"右膝",style:"top:1030rpx;left:280rpx;width:90rpx;height:70rpx;border-radius:20rpx;"},
          {name:"左小腿",style:"top:1100rpx;right:270rpx;width:90rpx;height:290rpx;z-index:5;",left:true},
          {name:"右小腿",style:"top:1100rpx;left:280rpx;width:90rpx;height:290rpx;z-index:5;"},
          {name:"左脚掌与脚趾",style:"bottom:10rpx;right:250rpx;width:110rpx;height:100rpx;border-radius:0 0 0 100rpx;",left:true},
          {name:"右脚掌与脚趾",style:"bottom:10rpx;left:260rpx;width:110rpx;height:100rpx;border-radius:0 0 100rpx 0;"},
        ],
        afterList:[
          {name:"右耳",style:"top:110rpx;left:290rpx;width:20rpx;height:50rpx;z-index:5;"},
          {name:"左耳",style:"top:110rpx;right:300rpx;width:20rpx;height:50rpx;z-index:5;"},
          {name:"头后方",style:"top:20rpx;left:300rpx;width:140rpx;height:170rpx;border-radius:80rpx;"},
          {name:"颈椎",style:"top:190rpx;left:320rpx;width:100rpx;height:50rpx;"},
          {name:"左肩",style:"top:270rpx;left:180rpx;width:100rpx;height:140rpx;border-radius:120rpx 20rpx 90rpx 60rpx;z-index:9;"},
          {name:"右肩",style:"top:270rpx;right:190rpx;width:90rpx;height:140rpx;border-radius:20rpx 90rpx 20rpx 80rpx  ;z-index:9;"},
          {name:"左上背",style:"top:250rpx;left:280rpx;width:95rpx;height:140rpx;z-index:9;"},
          {name:"右上背",style:"top:250rpx;right:280rpx;width:95rpx;height:140rpx;z-index:9;"},
          {name:"胸椎",style:"top:395rpx;right:280rpx;width:200rpx;height:40rpx;z-index:9;"},
          {name:"腰椎",style:"top:440rpx;right:280rpx;width:200rpx;height:40rpx;z-index:9;"},
          {name:"左下背",style:"top:485rpx;left:265rpx;width:110rpx;height:110rpx;z-index:9;"},
          {name:"右下背",style:"top:485rpx;right:265rpx;width:110rpx;height:110rpx;z-index:9;"},
          {name:"左手肘",style:"top:500rpx;left:170rpx;width:90rpx;height:70rpx;z-index:9;"},
          {name:"右手肘",style:"top:500rpx;right:170rpx;width:90rpx;height:70rpx;z-index:9;"},
          {name:"臀部",style:"top:600rpx;left:240rpx;width:270rpx;height:220rpx;z-index:9;"},
          {name:"右手掌",style:"top:760rpx;right:50rpx;width:90rpx;height:150rpx;transform: rotate(-20deg);border-radius:0 0 100rpx  90rpx;"},
          {name:"左手掌",style:"top:760rpx;left:50rpx;width:90rpx;height:150rpx;transform: rotate(20deg);border-radius:0 0 100rpx  90rpx ;"},
          {name:"右大腿",style:"top:810rpx;right:240rpx;width:130rpx;height:220rpx;"},
          {name:"左大腿",style:"top:810rpx;left:240rpx;width:130rpx;height:220rpx;"},
          {name:"右膝",style:"top:1030rpx;right:280rpx;width:90rpx;height:70rpx;border-radius:20rpx;"},
          {name:"左膝",style:"top:1030rpx;left:280rpx;width:90rpx;height:70rpx;border-radius:20rpx;"},
          {name:"右小腿",style:"top:1100rpx;right:270rpx;width:90rpx;height:290rpx;z-index:5;"},
          {name:"左小腿",style:"top:1100rpx;left:280rpx;width:90rpx;height:290rpx;z-index:5;"},
          {name:"右脚掌与脚趾",style:"bottom:10rpx;right:250rpx;width:110rpx;height:80rpx;border-radius:0 100rpx 0  0;"},
          {name:"左脚掌与脚趾",style:"bottom:10rpx;left:280rpx;width:110rpx;height:80rpx;border-radius: 100rpx 0 0  0 ;"},
        ],
        pain_part:[],
        current:12,
        partscurrent:0,
        doctorList:[],
        resultList:[{cn: '疼痛部位', key: 'pain_part', value: []},
        { cn: '过去24小时内您疼痛最剧烈的程度', key: 'most_violent_degree', value: ''},
        { cn: '过去24小时内您疼痛最轻微的程度。', key: 'slightest_degree', value: ''},  
        { cn: '过去24小时内疼痛的平均程度。', key: 'average_degree_pain', value: ''}, 
        { cn: '目前的疼痛程度', key: 'current_pain_level', value: ''},
        { cn: '疼痛缓解的程度。', key: 'pain_relief', value: ''},  
        { cn: '过去24小时内疼痛对您日常生活的影响', key: 'Impact_daily_life', value: ''},  
        { cn: '24小时内疼痛对您情绪的影响', key: 'Impact_your_mood', value: ''},
        { cn: '过去24小时内疼痛对您行走能力的影响', key: 'influence_walking_ability', value: ''},  
        { cn: '过去24小时内疼痛对您日常工作的影响', key: 'impact_daily_work', value: ''},  
        { cn: '过去24小时内疼痛对您与他人关系的影响', key: 'Influence_relationship', value: ''},
        { cn: '过去24小时内疼痛对您睡眠的影响', key: 'effects_sleep', value: ''},  
        { cn: '过去24小时内疼痛对您生活兴趣的影响', key: 'Influence_life_interest', value: ''}],
        user: [],
        needSetting:app.globalData.needAuthorization
    },
    onShareAppMessage: function () {
      return {
        title: '慢性疼痛综合评估量表(专业版)',
        path: '/subuser/pages/research/index',
        imageUrl: '',
      }
    },
    onLoad: function (opt) {
      let that = this
      // var newObj = JSON.parse(JSON.stringify(q));
      that.setData({
        type:opt.type ?opt.type:'',
        user:q.q,
        treatment_item_id:opt.treatment_item_id?opt.treatment_item_id:'',
        week:opt.week?Number(opt.week):''
      })
      let xUser = JSON.parse(JSON.stringify(q.q))
      if(!app.globalData.user.phone_number){
        xUser.push( { cn: '获取手机号', key: 'phone_number', value: '', type: 'phone', step:6})
      }
      xUser.push( { cn: '疾病诊断治疗经历', key: 'diagnostic_experience', value: '', type: 'textarea', step:6})
      if( app.globalData.user.hospital_id && !app.globalData.user.doctor_id && app.globalData.user.hospital_id !='undefined'){
        xUser.push( { cn: '选择你的主治医生', key: 'doctor_id', value: '', type: 'pickerDoctor', step:6})
        that.xGetDoctor()
      }
      that.setData({
        user: xUser
      })
    },
    xGetDoctor:function(){
      let that = this
      wx.cloud.callFunction({
        name: 'doctors',
        data: {
          action: "get_doctor_by_hospital",
          hospital_id:app.globalData.user.hospital_id
        }
      }).then(res => {
        that.setData({
          doctorList:res.result
        })
      })
    },
    xToChoosePlan:function(){
      wx.redirectTo({
        url: '/subsale/pages/choosePlan/index/index',
      })
    },
    chooseparts:function(e){
      let that =this
      let name = e.currentTarget.dataset.name
      let current = e.currentTarget.dataset.index
      for(let index in that.data.user[0].value){
        if(that.data.user[0].value[index] == name){
            that.data.user[0].value.splice(index,1)
            this.setData({
              [`user[0].value`]:that.data.user[0].value
            })
            return
        }
      }
      that.data.user[0].value.push(name)
      this.setData({
        [`user[0].value`]:that.data.user[0].value,
        current:current
      })
    },
    setpartsC:function(e){
      let current = e.currentTarget.dataset.index
      this.setData({
        partscurrent:current
      })
    },
    chooseS:function(e){
      let that = this
      let params = e.currentTarget.dataset
      that.setData({
        [`user[${params.idx}].range[${params.i}].check`]:!that.data.user[params.idx].range[params.i].check,
      })
      if(that.data.user[params.idx].range[params.i].check){
        that.data.user[params.idx].value.push(params.value)
      }else{
        for(let ii in that.data.user[params.idx].value){
          if(that.data.user[params.idx].value[ii]== params.value){
            that.data.user[params.idx].value.splice(ii,1)
          }
        }
      }
      that.setData({
        [`user[${params.idx}]`]:that.data.user[params.idx]
      })
    },
    // 其他疼痛干啥输入
    xInputPain:function(e){
      let that = this
      let value = e.detail.value
      let index  = e.currentTarget.dataset.index
      if(value){
        that.data.user[index].value.push(value)
        that.setData({
          [`user[${index}].value`]:that.data.user[index].value
        })
      }
    },
    checkboxChange(e) {
      let that = this
      let value = e.detail.value
      let params = e.currentTarget.dataset
      that.setData({
          [`user[${params.idx}].value`]: value
      })
    },
    xregion:function(e){
      let that = this
      let value =' '
      if(e.detail.value[0] !==  e.detail.value[1]){
        value  = e.detail.value[0]+ e.detail.value[1] 
      }else{
        value = e.detail.value[0]
      }
      let params = e.currentTarget.dataset
      that.setData({
          [`user[${params.idx}].value`]: value
      })
    },
    sliderchange:function(e){
      let that = this
      let value = e.detail.value
      let params = e.currentTarget.dataset
      that.setData({
        [`user[${params.idx}].value`]: value,
        [`bgColor[${params.idx}]`]:'linear-gradient(to right, #D3F9CC 30%, #F2E5BF 60%, #F2CFD4 100%)'
      })
      if(e.detail.value<=(params.max/5)){
        that.setData({
          [`bgColorS[${params.idx}]`]:'linear-gradient(to right,#23E201 50%,#97E201 100%)'
        })
      }else if( e.detail.value> (params.max/5)&& e.detail.value <= (params.max/5)*3){
        that.setData({
          [`bgColorS[${params.idx}]`]:'linear-gradient(to right,#23E201 50%,#FFBB00 100%)'
        })
      }else{
        that.setData({
          [`bgColorS[${params.idx}]`]:'linear-gradient(to right,#23E201 30%,#FFBB00 60%,#FF4F66 100%)'
        })
      }
    },
    xInput: function (e) {
        let that = this
        let value = e.detail.value
        let params = e.currentTarget.dataset
        that.setData({
            [`user[${params.idx}].value`]: value
        })
    },
    choose:function(e){
      let that = this
      let params = e.currentTarget.dataset
      that.setData({
        [`user[${params.idx}].value`]: params.i
      })
      that.xToStep()
    },
    xPicker: function (e) {
        let that = this
        let sel = e.detail.value
        let params = e.currentTarget.dataset
        let range = that.data.doctorList
        let value = range[sel]
        that.setData({
            [`user[${params.idx}].value`]: value
        })
    },
    xDate: function (e) {
        let that = this
        let value = e.detail.value
        let params = e.currentTarget.dataset
        that.setData({
            [`user[${params.idx}].value`]: value
        })
    },
    handleTouchInput:function(){
      this.setData({
        innerShow:true
      })
    },
    changePrivacySetting:function(e){
      this.setData({
        needSetting:false
      })
    },
    xPhone: async function (e) {
        let that = this
        let params = e.currentTarget.dataset
        wx.showLoading({
            title: '请稍等',
        })
        await wx.cloud.callFunction({
            name: 'user',
            data: {
                action: 'getPhone',
                cloudID: e.detail.cloudID
            },
            success: function (res) {
                that.setData({
                    [`user[${params.idx}].value`]: res.result.phoneNumber
                })
            }
        })

        wx.hideLoading()

    },
    xLocation: async function (e) {
        let that = this
        let params = e.currentTarget.dataset

        // wx.showLoading({
        //     title: '请稍等',
        // })

        // await wx.getLocation({
        //     type: 'wgs84',
        //     success(res) {
        //         const latitude = res.latitude
        //         const longitude = res.longitude
        //         const speed = res.speed
        //         const accuracy = res.accuracy
        //         wx.request({
        //             url: `https://apis.map.qq.com/ws/geocoder/v1/?key=CQ3BZ-OXQKP-7RLDJ-L4FP5-VLIIH-ENFXZ&location=` + latitude + ',' + longitude,
        //             success: res => {
        //                 let address = res.data.result.address_component
        //                 let city = address.city
        //                 let area = address.district
        //                 that.setData({
        //                     [`user[${params.idx}].value`]: city + '-' + area
        //                 })
        //             }
        //         })
        //     }
        // })

        wx.hideLoading()

    },
    xToBack:function(e){
      let that =this
      let index = e.currentTarget.dataset.index
      let step = e.currentTarget.dataset.step
      that.setData({
        first:step,
        animation_class: 'animation_class_before'
      })
      wx.pageScrollTo({
        scrollTop: 0,
      })
    },
    xToStep:function(e){
      let that =this
      let index = that.data.first
      let step = that.data.first+1
      if( that.data.user[index].value ==='' || that.data.user[index].value.length==0 ){
        wx.showToast({
          title: '当前内容未填写，请检查',
          icon:'none'
        })
        return
      }
      if(step < that.data.user.length){
        setTimeout(function(){
          that.setData({
            first:step,
            animation_class: 'animation_class'
          })
          wx.pageScrollTo({
            scrollTop: 0,
          })
        },600)
      }
    },
    xChildReload: function () {
      var pages = getCurrentPages();//当前页面栈
      if (pages.length > 1) {
        var beforePage = pages[pages.length - 2];//获取上一个页面实例对象
        beforePage.xInfo();//触发父页面中的方法
      }
    },
    xSubmit: function () {
        let that = this
        let user = that.data.user
        let key =0
        for (let i in that.data.user) {
          if (that.data.user[i].value === ''  && that.data.user[i].step== that.data.first || that.data.user[i].value.length==0  && that.data.user[i].step== that.data.first) {
              key=key+1
          }
        }
        if(key>0){
          var firstQ = ''
          for (var i =0;i<that.data.user.length;i++) {
            if (that.data.user[i].value === '') {
              firstQ=i
              break;
            }
          }
          wx.showModal({
            title: "提示",
            content: "请确保所有内容填写正确，当前有"+ key +"项内容未填写，请检查！",
            showCancel: false,
            confirmText:'好的',
            success (res) {
              if (res.confirm) {
                wx.pageScrollTo({
                  selector: '#selector'+firstQ,
                })
              }
            }
          })
          return
        }
        wx.showLoading({
          title: '保存中',
        })
        that.xUpload(user)
    },
    xUpload: function (data) {
        let that =this;
        let form = {}
        let resultList = that.data.resultList
        for (var i = 0; i < data.length; i++) {
            if (data[i].value !== '') {
                form[data[i].key] = data[i].value
            }
        }
        form.treatment_item_id = that.data.treatment_item_id
        form.week = that.data.week
        wx.cloud.callFunction({
            name: "evaluation",
            data: {
                action: "add_mixed_standardscoles",
                dataObj: form
            }
        }).then(res => {
          wx.hideLoading()
          res.result.create_time = util.formatTime_chinese_m(new Date(res.result.create_time))
          res.result.pri_desc = text.text(res.result.pri_desc)
          let newstr=''
          if(app.globalData.user.phone_number){
            const strAry = app.globalData.user.phone_number.split('');
            strAry[3] = 'x';
            strAry[4] = 'x';
            strAry[5] = 'x';
            strAry[6] = 'x';
            newstr =  strAry.join("")
          }
          that.setData({
            result:res.result,
            xPageReady:'end',
            resultList:resultList,
            phone_number:newstr
          })
          that.xChildReload()
        })
    },
    xBack:function(){
      wx.reLaunch({
        url: '/pages/navigation/using/index',
      })
    }
})